﻿using System;
using System.Data;
using System.Collections.Generic;
namespace Model.Sec
{
    /// <summary>
    /// 用户类，一个实例是用户的抽象表示
    /// </summary>
    public class User
    {
        #region User的构造方法
        /// <summary>
        /// 构造用户实例
        /// </summary>
        public User()
        {
        }
        #endregion

        #region User的属性
        private string _UserISN;
        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserISN
        {
            get { return _UserISN; }
            set { _UserISN = value; }
        }
        private string _UnitISN;
        /// <summary>
        /// 用户所属的单位编码
        /// </summary>
        public string UnitISN
        {
            get { return _UnitISN; }
            set { _UnitISN = value; }
        }
        private string _UserName;
        /// <summary>
        /// 用户真实姓名
        /// </summary>
        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }
        private string _Password;
        /// <summary>
        /// 用户密码
        /// </summary>
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }
        private string _UnitName;

        public string UnitName
        {
            get { return _UnitName; }
            set { _UnitName = value; }
        }
        private DateTime _CreateTime;
        /// <summary>
        /// 用户产生时间
        /// </summary>
        public DateTime CreateTime
        {
            get { return _CreateTime; }
            set { _CreateTime = value; }
        }
        private string _Position;
        /// <summary>
        /// 用户职位
        /// </summary>
        public string Position
        {
            get { return _Position; }
            set { _Position = value; }
        }
        private string _Tel;
        /// <summary>
        /// 联系电话
        /// </summary>
        public string Tel
        {
            get { return _Tel; }
            set { _Tel = value; }
        }
        private string _EMail;
        /// <summary>
        /// Email
        /// </summary>
        public string EMail
        {
            get { return _EMail; }
            set { _EMail = value; }
        }
        private string _IPSection;
        /// <summary>
        /// 允许登录网段
        /// </summary>
        public string IPSection
        {
            get { return _IPSection; }
            set { _IPSection = value; }
        }
        private int _Sort;
        /// <summary>
        /// 显示顺序
        /// </summary>
        public int Sort
        {
            get { return _Sort; }
            set { _Sort = value; }
        }
       
        private Unit _Unit;
        /// <summary>
        /// 用户所属单位
        /// </summary>
        public Unit Unit
        {
            get
            {
                if (_Unit == null)
                {
                    _GetUnitModel();
                }
                return _Unit;
            }
        }
        private Node _Node;
        /// <summary>
        /// 用户所属节点
        /// </summary>
        public Node UserNode
        {
            get
            {
                if (_Node == null)
                {
                    _GetNodeModel();
                }
                return _Node;
            }
        }
        private List<UserGroup> _UserGroup;
        /// <summary>
        /// 用户所属的用户组Model
        /// </summary>
        public List<UserGroup> UserGroup
        {
            get
            {
                if (this._UserGroup == null)
                    this.GetUserGroup();
                return this._UserGroup;
            }
        }

        private string _UserGroupISN;
        /// <summary>
        /// 用户所属的用户组,以逗号分开
        /// </summary>
        public string UserGroupISN
        {
            get
            {
                if (this._UserGroupISN == null)
                    this.GetUserGroupISN();
                return this._UserGroupISN;
            }
        }

        //private bool _PropertyInitialized = false;
        ///// <summary>
        ///// 是否已初始化所有属性
        ///// </summary>
        //public bool PropertyInitialized
        //{
        //    get { return this._PropertyInitialized; }
        //    set { this._PropertyInitialized = value; }
        //}    
        #endregion

        #region 私有方法
        /// <summary>
        /// 读取单位，和DAL层的Unit类下的GetModel方法应该是一模一样的
        /// </summary>
        private void _GetUnitModel()
        {
            System.Data.DataRow dr = null;
            string sql = "SELECT * FROM SEC_UNIT WHERE UNITISN='" + UnitISN + "'";
            System.Data.DataSet ds = DBUtility.OracleHelper.Query(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                Model.Sec.Unit unit = new Model.Sec.Unit();
                unit.UnitISN = UnitISN;
                unit.UpNodeName = dr["UPNODENAME"].ToString();
                unit.Address = dr["ADDRESS"].ToString();
                if (dr["CREATETIME"].ToString() != "")
                    unit.Createtime = Convert.ToDateTime(dr["CREATETIME"]);
                unit.Description = dr["DESCRIPTION"].ToString();
                unit.Tel = dr["TEL"].ToString();
                unit.UnitName = dr["UNITNAME"].ToString();
                unit.UnitSort = dr["UNITSORT"].ToString();
                //this.UnitType = dr["UNITTYPE"].ToString();
                unit.UpNodeID = dr["UPNODEID"].ToString();
                //unit.PropertyInitialized = true;

                this._Unit = unit;
            }
        }
        private void _GetNodeModel()
        {
            DataRow dr = null;
            string sql = "select sec_node.* from sec_Node,sec_user_node where sec_user_node.userisn='" + this.UserISN + "' and sec_user_node.nodeisn = sec_node.nodeisn";
            DataSet ds = DBUtility.OracleHelper.Query(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];

                Model.Sec.Node node = new Model.Sec.Node();
                node.NodeISN = dr["NODEISN"].ToString();
                node.NodeID = dr["NODEID"].ToString();
                node.NodeName = dr["NODENAME"].ToString();
                if (dr["CREATETIME"].ToString() != "")
                    node.CreateTime = Convert.ToDateTime(dr["CREATETIME"]);
                node.NodeLevel = dr["NODELEVEL"].ToString();
                node.UpNodeID = dr["UPNODEID"].ToString();
                node.UpNodeName = dr["UPNODENAME"].ToString();
                node.NodeType = dr["NODETYPE"].ToString();
                node.NodeIndex = Convert.ToInt32(dr["NODEINDEX"].ToString());
                node.LNG = float.Parse(dr["LNG"].ToString());
                node.LAT = float.Parse(dr["LAT"].ToString());
                node.IsCatalog = Convert.ToInt32(dr["ISCATALOG"]);
                node.DisplayLevel = dr["DISPLAYLEVEL"].ToString();
                node.Strip = dr["STRIP"].ToString();
                this._Node = node;
            }
        }
        private void GetUserGroupISN()
        {
            string UserGroupISN = null;
            string sql = "select * from sec_usergroup_userinfo where userisn='" + this.UserISN + "'";
            DataTable  dt = DBUtility.OracleHelper.Query(sql).Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                UserGroupISN += row["UserGroupISN"].ToString() + ",";
            }
            this._UserGroupISN = UserGroupISN;
        }
        private void GetUserGroup()
        {

            List<UserGroup> list = new List<UserGroup>();
            DataRow dr = null;
            string sql = "SELECT SU.CreateTime,SU.Description,SU.UserGroupISN,SU.UserGroupName,SU.SortIndex FROM SEC_USERGROUP SU,sec_usergroup_userinfo SUU  WHERE SUU.userisn ='" + this.UserISN + "'and SU.usergroupisn=SUU.usergroupisn";
            DataSet ds = DBUtility.OracleHelper.Query(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                Model.Sec.UserGroup userGroup = new Model.Sec.UserGroup();
                if (dr["CREATETIME"].ToString() != "")
                    userGroup.CreateTime = Convert.ToDateTime(dr["CREATETIME"]);
                userGroup.Description = dr["DESCRIPTION"].ToString();
                userGroup.UserGroupISN = dr["USERGROUPISN"].ToString();
                userGroup.UserGroupName = dr["USERGROUPNAME"].ToString();
                userGroup.SortIndex = dr["SORTINDEX"].ToString();
                list.Add(userGroup);
            }
            this._UserGroup = list;

        }
        #endregion
    }
}
